/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GuiDadosMotorista.java
 *
 * Created on 24/11/2011, 21:45:31
 */
package view.gui;

import javax.swing.JOptionPane;
import model.dominio.Motorista;
import controller.ControllerMotorista;

/**
 *
 * @author Leandro
 */
public class GuiDadosMotorista  extends JInternalFramePermissao {

    private Motorista motorista;

    /** Creates new form GuiDadosMotorista */
    public GuiDadosMotorista() {
        initComponents();
        motorista = new Motorista();
    }

    /**
     * Metodo onde armazena os dados digitado no programa
     * @param m Motorista contendo filtros do cadastro
     */
    public GuiDadosMotorista(Motorista m) {
        initComponents();

        boxNome.setText(m.getNome());
        boxCpf.setText(m.getCpf());
        boxCnh.setText(m.getCnh());
        boxFone.setText(m.getTelefone());
        boxEnd.setText(m.getEndereco());
        jButton1.setText("Editar");
        motorista = m;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        boxNome = new javax.swing.JTextField();
        boxEnd = new javax.swing.JTextField();
        boxCnh = new javax.swing.JTextField();
        boxCpf = new javax.swing.JFormattedTextField();
        boxFone = new javax.swing.JFormattedTextField();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setClosable(true);
        setTitle("Cadastro do Motorista");
        getContentPane().setLayout(null);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados do Motorista"));
        jPanel1.setLayout(null);

        jLabel1.setText("Nome*:");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(10, 30, 70, 30);

        jLabel2.setText("CPF*:");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(10, 70, 50, 30);

        jLabel3.setText("CNH*:");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(230, 70, 40, 30);

        jLabel4.setText("Endereço:");
        jPanel1.add(jLabel4);
        jLabel4.setBounds(10, 110, 80, 30);

        jLabel5.setText("Telefone:");
        jPanel1.add(jLabel5);
        jLabel5.setBounds(10, 150, 80, 30);

        boxNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxNomeActionPerformed(evt);
            }
        });
        jPanel1.add(boxNome);
        boxNome.setBounds(100, 30, 300, 30);

        boxEnd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxEndActionPerformed(evt);
            }
        });
        jPanel1.add(boxEnd);
        boxEnd.setBounds(100, 110, 300, 30);

        boxCnh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxCnhActionPerformed(evt);
            }
        });
        jPanel1.add(boxCnh);
        boxCnh.setBounds(270, 70, 130, 30);

        try {
            boxCpf.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        boxCpf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxCpfActionPerformed(evt);
            }
        });
        jPanel1.add(boxCpf);
        boxCpf.setBounds(100, 70, 110, 30);

        try {
            boxFone.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##) ####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        boxFone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxFoneActionPerformed(evt);
            }
        });
        jPanel1.add(boxFone);
        boxFone.setBounds(100, 150, 110, 30);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(0, 10, 410, 200);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/imagens/cancelar.png"))); // NOI18N
        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);
        jButton2.setBounds(220, 220, 140, 60);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/imagens/salvar.png"))); // NOI18N
        jButton1.setText("Cadastrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(50, 220, 140, 60);

        setBounds(0, 0, 428, 329);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed
    /**
     * Metodo para ler o que foi digitado nos campos,verificar se os campos
     * obrigatorios foram preenchidos e dizer se os dados do motorista foi ou
     * nao salvos com sucesso.
     * @param evt 
     */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        guardar();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void guardar() {
        if (boxNome.getText().isEmpty() || boxCpf.getText().isEmpty() || boxCnh.getText().isEmpty()) { // TODO errado o segundo isEmpty
            JOptionPane.showMessageDialog(null, "Campo(s) obrigatórios não foram preenchidos!", "ERRO", JOptionPane.ERROR_MESSAGE);
            return;
        }
        motorista.setNome(boxNome.getText());
        motorista.setEndereco(boxEnd.getText());
        motorista.setCpf(boxCpf.getText());
        motorista.setTelefone(boxFone.getText());
        motorista.setCnh(boxCnh.getText());
        // ControllerMotorista.salvarMotorista(motorista);
        if (ControllerMotorista.salvarMotorista(motorista)) {
            JOptionPane.showMessageDialog(null, "Dados salvos com sucesso!", "Salvar Motorista", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, "Erro ao salvar dados!", "Salvar Motorista", JOptionPane.ERROR_MESSAGE);
        }
        this.dispose();
    }
    private void boxCnhActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxCnhActionPerformed
        guardar();
    }//GEN-LAST:event_boxCnhActionPerformed

    private void boxNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxNomeActionPerformed
        guardar();
    }//GEN-LAST:event_boxNomeActionPerformed

    private void boxCpfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxCpfActionPerformed
        guardar();
    }//GEN-LAST:event_boxCpfActionPerformed

    private void boxEndActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxEndActionPerformed
        guardar();
    }//GEN-LAST:event_boxEndActionPerformed

    private void boxFoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxFoneActionPerformed
        guardar();
    }//GEN-LAST:event_boxFoneActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField boxCnh;
    private javax.swing.JFormattedTextField boxCpf;
    private javax.swing.JTextField boxEnd;
    private javax.swing.JFormattedTextField boxFone;
    private javax.swing.JTextField boxNome;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
    @Override
    public int getPermissao() {
        if(motorista.getCodigo()!= null && motorista.getCodigo()>0){
            return 21;
        }else{
            return 20;
        }
    }

}
